package com.gitee.qdbp.general.marketing.api.activity.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.gitee.qdbp.able.jdbc.paging.PartList;
import com.gitee.qdbp.base.annotation.DataIsolation;
import com.gitee.qdbp.base.annotation.OperateTraces;

/**
 * 活动主实体类
 *
 * @author zhh
 * @version 181114
 */
@OperateTraces(id = "id")
@DataIsolation(enable = false)
public class ActivityCoreBase implements Serializable {

    /** 版本序列号 **/
    private static final long serialVersionUID = 1L;

    /** 活动名称 **/
    private String activityName;

    /** 活动描述 **/
    private String activityIntro;

    /** 创建时间 **/
    private Date createTime;

    /** 生效时间 **/
    private Date enableTime;

    /** 失效时间 **/
    private Date disableTime;

    /** 每人次数限制 **/
    private Integer frequencyLimitation;

    /** 选项 **/
    private ActivityCoreOptions options;

    /** 获取活动名称 **/
    public String getActivityName() {
        return activityName;
    }

    /** 设置活动名称 **/
    public void setActivityName(String activityName) {
        this.activityName = activityName;
    }

    /** 获取活动描述 **/
    public String getActivityIntro() {
        return activityIntro;
    }

    /** 设置活动描述 **/
    public void setActivityIntro(String activityIntro) {
        this.activityIntro = activityIntro;
    }

    /** 获取创建时间 **/
    public Date getCreateTime() {
        return createTime;
    }

    /** 设置创建时间 **/
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /** 获取生效时间 **/
    public Date getEnableTime() {
        return enableTime;
    }

    /** 设置生效时间 **/
    public void setEnableTime(Date enableTime) {
        this.enableTime = enableTime;
    }

    /** 获取失效时间 **/
    public Date getDisableTime() {
        return disableTime;
    }

    /** 设置失效时间 **/
    public void setDisableTime(Date disableTime) {
        this.disableTime = disableTime;
    }

    /** 获取每人次数限制 **/
    public Integer getFrequencyLimitation() {
        return frequencyLimitation;
    }

    /** 设置每人次数限制 **/
    public void setFrequencyLimitation(Integer frequencyLimitation) {
        this.frequencyLimitation = frequencyLimitation;
    }

    /** 获取选项 **/
    public ActivityCoreOptions getOptions() {
        return options;
    }

    /** 获取选项, force=是否强制返回非空对象 **/
    public ActivityCoreOptions getOptions(boolean force) {
        if (options == null && force) {
            options = new ActivityCoreOptions();
        }
        return options;
    }

    /** 设置选项 **/
    public void setOptions(ActivityCoreOptions options) {
        this.options = options;
    }

    /**
     * 将当前对象转换为子类对象
     *
     * @param clazz 目标类型
     * @return 目标对象
     */
    public <T extends ActivityCoreBase> T to(Class<T> clazz) {
        T instance;
        try {
            instance = clazz.newInstance();
        } catch (Exception e) {
            throw new IllegalArgumentException("Failed to create " + clazz.getSimpleName() + " instance.", e);
        }

        instance.setActivityName(this.getActivityName()); // 活动名称
        instance.setActivityIntro(this.getActivityIntro()); // 活动描述
        instance.setCreateTime(this.getCreateTime()); // 创建时间
        instance.setEnableTime(this.getEnableTime()); // 生效时间
        instance.setDisableTime(this.getDisableTime()); // 失效时间
        instance.setFrequencyLimitation(this.getFrequencyLimitation()); // 每人次数限制
        instance.setOptions(this.getOptions()); // 选项
        return instance;
    }

    /**
     * 将ActivityCoreBean转换为子类对象
     *
     * @param beans 待转换的对象列表
     * @param clazz 目标类型
     * @param <T> ActivityCoreBean或子类
     * @param <C> T的子类
     * @return 目标对象列表
     */
    public static <T extends ActivityCoreBase, C extends T> List<C> to(List<T> beans, Class<C> clazz) {
        if (beans == null) {
            return null;
        }
        List<C> list;
        if (beans instanceof PartList) {
            PartList<C> partlist = new PartList<>();
            partlist.setTotal(((PartList<?>) beans).getTotal());
            list = partlist;
        } else {
            list = new ArrayList<>();
        }
        for (ActivityCoreBase bean : beans) {
            list.add(bean.to(clazz));
        }
        return list;
    }

}